package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.model.User;
import com.conversationboard.model.userdata.UserData;

@WebServlet(name = "AdminCloneUsersControllerServlet", urlPatterns = "/Admin/AdminCloneUsersControllerServlet")
public class AdminCloneUsersControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -7780476932155978151L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {

			User user = User.get(request.getUserPrincipal().getName());

			if (!user.isSiteAdmin()) {
				return;
			}

			String checkUserId = request.getParameter("userid");
			User checkUser = User.get(checkUserId);

			List<UserData> cloneUsers = User.getUsersWithSamePassword(checkUser);

			request.setAttribute("checkUser", checkUser);
			request.setAttribute("users", cloneUsers);

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/Admin/cloneusers.jsp");

			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
